﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dry.DbInterpreter;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Dry.Generators
{
    public class MasterPageGenerator : GeneratorBase
    {
        public static GeneratorFeedback CreateFile(string strMasterPageName)
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            int lines = 0;

            // The .Master file
            string strCode = GetMasterPageMasterFile(strMasterPageName);
            generatorFeedback.Exception = CreateFileFromString("MasterPages\\AutoGenerated\\" + strMasterPageName + ".Master", strCode);
            lines += CountLinesInString(strCode);

            // The .Master.Designer.cs file
            strCode = GetMasterPageDesignerFile(strMasterPageName);
            generatorFeedback.Exception = CreateFileFromString("MasterPages\\AutoGenerated\\" + strMasterPageName + ".Master.designer.cs", strCode);
            lines += CountLinesInString(strCode);

            // The Master.cs file
            strCode = GetMasterPageClass(strMasterPageName);
            generatorFeedback.Exception = CreateFileFromString("MasterPages\\AutoGenerated\\" + strMasterPageName + ".Master.cs", strCode);
            lines += CountLinesInString(strCode);

            generatorFeedback.Lines = lines;

            return generatorFeedback;
        }

        private static string AddDesignerControlCode(string controlNamespace, string controlType, string controlName)
        {
            string strCode = string.Empty;

            strCode += "            /// <summary>" + _strNewLine;
            strCode += "            /// head control." + _strNewLine;
            strCode += "            /// </summary>" + _strNewLine;
            strCode += "            /// <remarks>" + _strNewLine;
            strCode += "            /// Auto-generated field." + _strNewLine;
            strCode += "            /// To modify move field declaration from designer file to code-behind file." + _strNewLine;
            strCode += "            /// </remarks>" + _strNewLine;
            strCode += "            protected global::" + controlNamespace + "." + controlType + " " + controlName + ";" + _strNewLine;

            return strCode;
        }

        public static string GetMasterPageDesignerFile(string strMasterPageName)
        {
            string strCode = string.Empty;

            strCode += "        //------------------------------------------------------------------------------" + _strNewLine;
            strCode += "        // <auto-generated>" + _strNewLine;
            strCode += "        //     This code was generated by a tool." + _strNewLine;
            strCode += "        //" + _strNewLine;
            strCode += "        //     Changes to this file may cause incorrect behavior and will be lost if" + _strNewLine;
            strCode += "        //     the code is regenerated. " + _strNewLine;
            strCode += "        // </auto-generated>" + _strNewLine;
            strCode += "        //------------------------------------------------------------------------------" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        namespace " + Dry.ProjectName + ".UI {" + _strNewLine;
            strCode += "            " + _strNewLine;
            strCode += "            " + _strNewLine;
            strCode += "            public partial class " + strMasterPageName + " {" + _strNewLine;
            strCode += "                " + _strNewLine;

            strCode += AddDesignerControlCode("System.Web.UI.HtmlControls", "HtmlForm", "frmTaxibokning");
            strCode += AddDesignerControlCode("System.Web.UI.WebControls", "ContentPlaceHolder", "cphHead");
            strCode += AddDesignerControlCode("System.Web.UI.WebControls", "ContentPlaceHolder", "cphHeader");
            strCode += AddDesignerControlCode("System.Web.UI.WebControls", "ContentPlaceHolder", "cphMenu");
            strCode += AddDesignerControlCode("System.Web.UI.WebControls", "ContentPlaceHolder", "cphContent");
            strCode += AddDesignerControlCode("System.Web.UI.WebControls", "ContentPlaceHolder", "cphFooter");

            strCode += "            }" + _strNewLine;
            strCode += "        }" + _strNewLine;
            
            return strCode;
        }

        public static string GetMasterPageClass(string strMasterPageName)
        {
            string strCode = string.Empty;

            strCode += GetFileHeader(Dry.ProjectName, "", "", "", "", "");

            strCode += "        using System;" + _strNewLine;
            strCode += "        using System.Collections.Generic;" + _strNewLine;
            strCode += "        using System.Globalization;" + _strNewLine;
            strCode += "        using System.Threading;" + _strNewLine;
            strCode += "        using System.Web;" + _strNewLine;
            strCode += "        using System.Web.Security;" + _strNewLine;
            strCode += "        using System.Web.UI.WebControls;" + _strNewLine;
            strCode += "        using Backend.BLL;" + _strNewLine;
            strCode += "        using Backend.BO;" + _strNewLine;
            strCode += "        using Backend.Enums;" + _strNewLine;
            strCode += "        using Backend.IP;" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        namespace " + Dry.ProjectName + ".UI" + _strNewLine;
            strCode += "        {" + _strNewLine;
            strCode += "            public partial class " + strMasterPageName + " : System.Web.UI.MasterPage" + _strNewLine;
            strCode += "            {" + _strNewLine;
            strCode += "                protected void Page_Load(object sender, EventArgs e)" + _strNewLine;
            strCode += "                {" + _strNewLine;
            strCode += "                }" + _strNewLine;
            strCode += "            }" + _strNewLine;
            strCode += "        }" + _strNewLine;

	        return strCode;
        }

        public static string GetMasterPageMasterFile(string strMasterPageName)
        {
            string strCode = string.Empty;

            strCode += "        <%@ Master Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"MasterPage.Master.cs\" Inherits=\"" + Dry.ProjectName + ".UI." + strMasterPageName + "\" %>" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        <html xmlns=\"http://www.w3.org/1999/xhtml\" >" + _strNewLine;
            strCode += "            <head runat=\"server\">" + _strNewLine;
            strCode += "            <title>" + Dry.ProjectName + "</title>" + _strNewLine;
            strCode += "            <asp:ContentPlaceHolder ID=\"cphHead\" runat=\"server\">" + _strNewLine;
            strCode += "            </asp:ContentPlaceHolder>" + _strNewLine;
            strCode += "        " + _strNewLine;
            strCode += "        </head>" + _strNewLine;
            strCode += "            <body>" + _strNewLine;
            strCode += "                <form id=\"frm" + Dry.ProjectName + "\" runat=\"server\">" + _strNewLine;
            strCode += "                    <div class=\"page\">" + _strNewLine;

            strCode += "                        <div class=\"header\">" + _strNewLine;
            strCode += "                            <asp:ContentPlaceHolder ID=\"cphHeader\" runat=\"server\">" + _strNewLine;
            strCode += "                            </asp:ContentPlaceHolder>" + _strNewLine;
            strCode += "                        </div>" + _strNewLine;

            strCode += "                        <div class=\"menu\">" + _strNewLine;
            strCode += "                            <asp:ContentPlaceHolder ID=\"cphMenu\" runat=\"server\">" + _strNewLine;
            strCode += "                            </asp:ContentPlaceHolder>" + _strNewLine;
            strCode += "                        </div>" + _strNewLine;

            strCode += "                        <div class=\"content\">" + _strNewLine;
            strCode += "                            <asp:ContentPlaceHolder ID=\"cphContent\" runat=\"server\">" + _strNewLine;
            strCode += "                            </asp:ContentPlaceHolder>" + _strNewLine;
            strCode += "                        </div>" + _strNewLine;

            strCode += "                        <div class=\"footer\">" + _strNewLine;
            strCode += "                            <asp:ContentPlaceHolder ID=\"cphFooter\" runat=\"server\">" + _strNewLine;
            strCode += "                            </asp:ContentPlaceHolder>" + _strNewLine;
            strCode += "                        </div>" + _strNewLine;

            strCode += "                    </div>" + _strNewLine;
            strCode += "                </form>" + _strNewLine;
            strCode += "            </body>" + _strNewLine;
            strCode += "        </html>";

            return strCode;
        }
    }
}
